package TUILayer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ModelLayer.DataStorage;
import ModelLayer.RealException;

public class StartClass {
	
	public static void main(String args[]) throws RealException,
			ClassNotFoundException, IOException, InterruptedException {
		JFrame frame = new JFrame("System");
		frame.add(new JLabel("Output"), BorderLayout.NORTH);
		frame.add(new JLabel("Input"), BorderLayout.SOUTH);
		JTextArea ta = new JTextArea(15, 30);
		ta.setEditable(false);
		JTextField tf = new JTextField();
		frame.add(tf, BorderLayout.SOUTH);
		TextAreaOutputStream taos = new TextAreaOutputStream(ta);
		PrintStream ps = new PrintStream(taos);

		TextFieldStreamer is = new TextFieldStreamer(tf);
		tf.addActionListener(is);

		System.setIn(is);

		System.setOut(ps);
		System.setIn(is);
		System.setErr(ps);

		frame.setVisible(true);

		frame.add(new JScrollPane(ta));
		frame.pack();
		ta.setFocusable(false);
		tf.setVisible(true);

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		// Determine the new location of the window
		int w = frame.getSize().width;
		int h = frame.getSize().height;
		int x = (dim.width - w) / 2;
		int y = (dim.height - h) / 2;

		// Move the window
		frame.setLocation(x, y);

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				try {
					DataStorage dataStorage = new DataStorage();
					dataStorage.saveAll();
				} catch (IOException e) {
				}
				System.exit(1);
			}
		});

		DataStorage dataStorage = new DataStorage();

		try {
			dataStorage.loadAll();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}

		new MainMenuUI();

	}
}
